package com.ctgu.j08.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctgu.j08.common.enums.ResultEnum;
import com.ctgu.j08.mapper.DeviceMapper;
import com.ctgu.j08.service.DeviceService;
import com.ctgu.j08.utils.ResultUtil;
import com.ctgu.j08.vo.Device;
import com.ctgu.j08.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @version 1.0
 * @author: Saiya Wang
 * @date: 2021/5/29 15:32
 */
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService {
    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public Device getById(int deId) {
        Device device = deviceMapper.selectByPrimaryKey(deId);
        return device;
    }

    @Override
    public List<Device> getByDeName(String deName) {
        List<Device> deviceList = deviceMapper.getByDeviceName(deName);
        return deviceList;
    }

    @Override
    public Result editDevice(Device device) {
        int isEdit = deviceMapper.updateByPrimaryKey(device);
        if(isEdit != 0)
        {
            return ResultUtil.success(ResultEnum.SUCCESS);
        }else
        {
            System.out.println("更新失败"+isEdit);
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }

    @Override
    public Result addDevice(Device device) {
        int isAdd = deviceMapper.insertSelective(device);
        if(isAdd != 0)
        {
            return ResultUtil.success(ResultEnum.SUCCESS);
        }else
        {
            System.out.println("插入失败"+isAdd);
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }

    @Override
    public Result delDevice(Integer deId) {
        int isDel = deviceMapper.deleteByPrimaryKey(deId);
        if(isDel!=0)
        {
            System.out.println("删除成功："+deId);
            return ResultUtil.success(ResultEnum.SUCCESS);
        }else
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }

    @Override
    public Result getAllDeviceInfo() {
        List<Device> devices = deviceMapper.getAllDevice();
        if(devices == null)
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }else
        {
            System.out.println("部门列表："+devices);
            return ResultUtil.success(devices);
        }
    }

    @Override
    public Result searchDeviceByName(String deName) {
        List<Device> deviceList= deviceMapper.getByDeviceName(deName);
        if(deviceList != null)
        {
            System.out.println("搜索到的用户"+deviceList);
            return ResultUtil.success(deviceList);
        }
        else
        {
            return ResultUtil.error(ResultEnum.FAIL);
        }
    }
}
